package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.query.UserQuery;
import com.itheima.mp.service.Userservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: XJH
 * @Description: TODO
 * @Date: 2025/6/13 21:05
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/users")
@Api(tags = "用户管理接口")
@RequiredArgsConstructor
public class UserController {

    private final Userservice userservice;

    @PostMapping
    @ApiOperation("新增用户接口")
    public void save( @RequestBody UserFormDTO userFormDTO) {
        User  user =  BeanUtil.copyProperties(userFormDTO, User.class);
        userservice.save(user);

    }
    @DeleteMapping("/{id}")
    @ApiOperation("删除用户接口")
    public void delete( @PathVariable  Long id) {
     userservice.removeById(id);
    }
//    @GetMapping("/{id}")
//    @ApiOperation("根据id查询用户接口")
//    public UserVO Select( @PathVariable  Long id) {
//       User user =    userservice.getById( id);
//        return BeanUtil.copyProperties(user, UserVO.class);
//    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户接口")
    public UserVO Select( @PathVariable  Long id) {

        return  userservice.queryUserandAddress(id);
    }
//    @GetMapping
//    @ApiOperation("根据id批量查询用户接口")
//    public List<UserVO> Select(@RequestParam List<Long> ids) {
//     //简单的查询
//       List<User> users =  userservice.listByIds(ids);
//        return BeanUtil.copyToList(users, UserVO.class);
//    }
    @GetMapping
    @ApiOperation("根据id批量查询用户接口")
    public List<UserVO> Select(@RequestParam List<Long> ids) {
        //复杂的查询
        return userservice.queryUserandAddressByIds(ids);
    }
    @PutMapping("{id}/deduction/{money}")
    @ApiOperation("扣减用户余额")
    public void deductBalance(@PathVariable("id") Long id, @PathVariable("money")Integer money){
        userservice.deductBalance(id, money);
    }
    @GetMapping("/list")
    @ApiOperation("根据复杂条件查询用户接口")
    public List<UserVO> query( UserQuery userQuery) {

     List<User> users = userservice.queryUsers(userQuery.getName(),  userQuery.getStatus(), userQuery.getMinBalance(), userQuery.getMaxBalance());
        return BeanUtil.copyToList(users, UserVO.class);
    }
    @GetMapping("/page")
    @ApiOperation("根据复杂条件分页查询用户接口")
    public PageDTO<UserVO> queryUserPage(UserQuery userQuery) {
        return  userservice.queryUserPage(userQuery);
    }

}
